package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baor.dao.CarDao;
import com.baor.domain.CarVo;
import com.baor.service.CarService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.baor.utils.DateUtil.now;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarDao carDao;


    @Override
    public AjaxResult<?> list(Map<String,Object> params) {
        int pageIndex = (int) params.get("pageIndex");
        int pageSize = (int) params.get("pageSize");
        params.put("pageIndex",(pageIndex - 1)* pageSize);
        Map<String,Object> res = carDao.findPageCount(params);
        List<CarVo> carList = carDao.carList(params);
        res.put("carList", carList);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> insert(CarVo carVo) {
        LambdaQueryWrapper<CarVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarVo::getCarParking,carVo.getCarParking()).eq(CarVo::getCarCooperate,carVo.getCarCooperate());
        List<CarVo> list = carDao.selectList(queryWrapper);
        long count = list.stream().filter(x -> x.getCarLicense().equals(carVo.getCarLicense())).count();
        if ( count > 0 ){
            return AjaxResult.error("车辆已经存在，请删除后重试");
        }
        carVo.setCarCreatetime(now());
        int row = carDao.insert(carVo);
        if (row > 0){
            return AjaxResult.success(carVo.getCarId());
        }else {
            return AjaxResult.error("添加失败");
        }
    }

    @Override
    public AjaxResult<?> update(CarVo carVo) {
        int row = carDao.updateById(carVo);
        if(row >0){
            return AjaxResult.success();
        }else{
            return  AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> delete(Integer carId) {
        return AjaxResult.success(carDao.deleteById(carId));
    }

//    @Override
//    public AjaxResult<?> search(CarVo carVo) {
//        return AjaxResult.success(carDao.)
//    }

    @Override
    public AjaxResult<?> info(Map<String,Object> params) {
        return AjaxResult.success(carDao.carList(params));
    }

    @Override
    public AjaxResult<?> selectParking() {
        return AjaxResult.success(carDao.selectParking());
    }

    @Override
    public AjaxResult<?> selectCooperate() {
        return AjaxResult.success(carDao.selectCooperate());
    }


}
